package cn.edu.pzhu.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.pzhu.pojo.FileMsg;
import cn.edu.pzhu.pojo.User;
import cn.edu.pzhu.service.FileMsgService;
import cn.edu.pzhu.service.imp.FileMsgServiceImp;
import cn.edu.pzhu.util.PageUtil;

/**
 * Servlet implementation class MyshareServlet
 */
@WebServlet("/showmylist")
public class MyshareServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyshareServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String page = request.getParameter("page");
		String num = request.getParameter("num");
		HttpSession session =  request.getSession();
		int p = 1, n = 10;
		if(page!=null) { //"-1"
			try {
				p = Integer.parseInt(page);
			} catch (Exception e) {
				p = 1;
			}
		}
		if(num!=null) {
			try {
				n = Integer.parseInt(num);
			} catch (Exception e) {
				n = 10;
			}
		}
		
		try {
			User user = (User)request.getSession().getAttribute("user");
			String username = user.getUsername();
			FileMsgService fms=new FileMsgServiceImp();
			List<FileMsg> list = fms.showFileListByUsername(username);//调用接口通过用户名查询文件
			request.getSession().setAttribute("prelist", list);
			List<FileMsg> pageList=PageUtil.splitList(list, p, n);//调用工具类切分列表
			request.getSession().setAttribute("list", pageList);
			String address="showmylist";
			//导航栏
			StringBuffer bar = PageUtil.createBar(list, p, n,address);
			request.getSession().setAttribute("bar", bar);
			request.getSession().setAttribute("num", num);
			response.sendRedirect("myshare.jsp");	
		} catch (Exception e) {		
			session.setAttribute("msg", "请先登录再使用此功能！");	
			session.setAttribute("url", "login.jsp");
			response.sendRedirect("error.jsp");
			return; //流程结束，后续代码不需要再执行...
		}	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
